package 数列区间最大值;

import java.util.*;
import java.io.*;

public class Main {
    //    输入一串数字，给你 M个询问，每次询问就给你两个数字 X,Y，要求你说出 X
//    到 Y这段区间内的最大数。
//1≤N≤105,1≤M≤106
    static int N = 100010, n, m;
    static int[] w = new int[N];
    static PII[] tr = new PII[4 * N];

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = in.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        String[] s1 = in.readLine().split(" ");
        for (int i = 1; i <= n; i++) {
            w[i] = Integer.parseInt(s1[i - 1]);
        }
        build(1, 1, n);
        while (m-- > 0) {
            String[] str = in.readLine().split(" ");
            int x = Integer.parseInt(str[0]);
            int y = Integer.parseInt(str[1]);
            out.write(query(1, x, y)+"\n");
        }
        out.flush();
    }

    private static int query(int u, int l, int r) {
        if (tr[u].l >= l && tr[u].r <= r) {
            return tr[u].max;
        }
        int mid = tr[u].l + tr[u].r >> 1;
        int maxv = Integer.MIN_VALUE;
        if (l <= mid) {
            maxv = Math.max(maxv, query(u << 1, l, r));
        }
        if (r >= mid + 1) {
            maxv = Math.max(maxv, query(u << 1 | 1, l, r));
        }
        return maxv;
    }

    private static void build(int u, int l, int r) {
        if (l == r) {
            tr[u] = new PII(l, r, w[l]);
        } else {
            tr[u] = new PII(l, r);
            int mid = l + r >> 1;
            build(u << 1, l, mid);
            build(u << 1 | 1, mid + 1, r);
            pushup(u);
        }
    }

    private static void pushup(int u) {
        tr[u].max = Math.max(tr[u << 1].max, tr[u << 1 | 1].max);
    }

}

class PII {
    int l, r, max;

    public PII(int l, int r, int max) {
        this.l = l;
        this.r = r;
        this.max = max;
    }

    public PII(int l, int r) {
        this.l = l;
        this.r = r;
    }
}